package com.kayt.cyberBar.company.business.dao.impl;

import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kayt.cyberBar.common.PageData;
import com.kayt.cyberBar.common.dao.EntityDao;
import com.kayt.cyberBar.company.business.dao.ICompanyBaseUnitMapDao;
import com.kayt.cyberBar.company.business.entity.po.BCompanyBaseUnitMap;

@Repository("iCompanyBaseUnitMapDao")
public class CompanyBaseUnitMapDaoImpl implements ICompanyBaseUnitMapDao {

	@Autowired
	private EntityDao entityDao;
	
	@Override
	public Long addUnitMap (BCompanyBaseUnitMap unitMap) throws SQLException {
		return this.entityDao.save(unitMap);
	}
	
	@Override
	public int deleteUnitMap (String unitMapId) {
		if (unitMapId != null) {
			Long id;
			try {
				id = Long.parseLong(unitMapId);
			} catch (Exception e) {
				return -1;
			}
			BCompanyBaseUnitMap unitMap = this.entityDao.findById(id, BCompanyBaseUnitMap.class);
			if (unitMap != null) {
				this.entityDao.deleteDetached(unitMap);
				return 0;
			}
		}
		return -1;
	}
	
	@Override
	public void updateUnitMap (BCompanyBaseUnitMap unitMap) throws SQLException {
		this.entityDao.update(unitMap);
	}
	
	@Override
	public BCompanyBaseUnitMap getUnitMapById (String unitMapId) {
		BCompanyBaseUnitMap unitMap = null;
		if (unitMapId != null) {
			Long id;
			try {
				id = Long.parseLong(unitMapId);
			} catch (Exception e) {
				return null;
			}
			unitMap = this.entityDao.findById(id, BCompanyBaseUnitMap.class);
		}
		return unitMap;
	}
	
	@Override
	public BCompanyBaseUnitMap getUnitMapByCode (String itemCode) throws SQLException {
		BCompanyBaseUnitMap unitMap = null;
		if (itemCode != null) {
			StringBuffer hql = new StringBuffer(" o.itemCode=?");
			unitMap = this.entityDao.getObj(BCompanyBaseUnitMap.class, hql.toString(), new Object[]{itemCode});
		}
		return unitMap;
	}
	
	@Override
	public PageData<BCompanyBaseUnitMap> getUnitMapList (String hql, Object[] params, LinkedHashMap<String, String> orderMap) throws SQLException {
		PageData<BCompanyBaseUnitMap> salePageData = this.entityDao.getPagingData(BCompanyBaseUnitMap.class, -1, -1, hql, params, orderMap);
		return salePageData;
	}
	
	@Override
	public List getUnitMapListByHql(int pageNum, int pageSize, String hql, Object[] params) throws SQLException {
		return this.entityDao.getPagingDataByHql(pageNum, pageSize, hql, params);
	}
	
	@Override
	public boolean checkValidate(String itemCode) throws SQLException {
		if (StringUtils.isNotBlank(itemCode)) {
			String hql = "select count(o.id) as count from CPublicUnitMap o where o.itemCode=? ";
	        List countListMap = this.entityDao.getPagingDataByHql(-1, -1, hql, new Object[]{itemCode});
	        if(countListMap != null && !countListMap.isEmpty()) {
	            Map countMap = (Map)countListMap.get(0);
	            Long tempCount = (Long)countMap.get("count");
	            if (tempCount == 0) {
	            	return true;
	            }
	        }
		}
		return false;
	}
}
